Page({
  data: {
    sunRotateY: 0,        // 太阳旋转角度
    planets: [],          // 行星数据
    orbits: [],           // 轨道数据
    animationId: null,    // 动画ID
    lastTime: 0,          // 上一帧时间
    rotationSpeed: 0.3,   // 旋转速度
    isDragging: false,    // 是否正在拖动
    startX: 0,           // 拖动起始X坐标
    currentRotation: 0    // 当前旋转角度
  },

  onLoad() {
    this.initSolarSystem();
    this.startAnimation();
  },

  onUnload() {
    this.stopAnimation();
  },

  // 初始化太阳系
  initSolarSystem() {
    const planets = [
      { id: 1, name: '水星', color: '#A9A9A9', image: './../../assets/images/banner2-01.png', radius: 60, speed: 0.04, angle: 0, size: 12 },
      { id: 2, name: '金星', color: '#FFA500', image: './../../assets/images/banner2-01.png', radius: 90, speed: 0.015, angle: 0, size: 16 },
      { id: 3, name: '地球', color: '#1E90FF', image: './../../assets/images/banner2-01.png', radius: 120, speed: 0.01, angle: 0, size: 16 },
      { id: 4, name: '火星', color: '#FF4500', image: './../../assets/images/banner2-01.png', radius: 150, speed: 0.008, angle: 0, size: 14 },
      { id: 5, name: '木星', color: '#F4A460', image: './../../assets/images/banner2-01.png', radius: 200, speed: 0.002, angle: 0, size: 28 },
      { id: 6, name: '土星', color: '#DAA520', image: './../../assets/images/banner2-01.png', radius: 250, speed: 0.0009, angle: 0, size: 24, hasRing: true },
      { id: 7, name: '天王星', color: '#AFEEEE', image: './../../assets/images/banner2-01.png', radius: 300, speed: 0.0004, angle: 0, size: 20 },
      { id: 8, name: '海王星', color: '#4682B4', image: './../../assets/images/banner2-01.png', radius: 350, speed: 0.0001, angle: 0, size: 20 }
    ];

    const orbits = planets.map(planet => ({
      id: planet.id,
      radius: planet.radius
    }));

    this.setData({ planets, orbits });
  },

  // 开始动画
  startAnimation() {
    this.updatePlanets();
    this.animate(Date.now());
  },

  // 停止动画
  stopAnimation() {
    if (this.data.animationId) {
      clearTimeout(this.data.animationId);
      this.data.animationId = null;
    }
  },

  // 动画循环
  animate(timestamp) {
    if (!this.data.lastTime) {
      this.data.lastTime = timestamp;
    }
    
    const deltaTime = timestamp - this.data.lastTime;
    this.data.lastTime = timestamp;
    
    if (!this.data.isDragging) {
      this.setData({
        sunRotateY: this.data.sunRotateY + this.data.rotationSpeed
      });
    }
    
    this.updatePlanets(deltaTime);
    
    // 使用setTimeout模拟requestAnimationFrame
    this.data.animationId = setTimeout(() => {
      this.animate(Date.now());
    }, 16); // 约60fps
  },

  // 更新行星位置
  updatePlanets(deltaTime) {
    const planets = this.data.planets.map(planet => {
      // 更新行星角度
      const newAngle = (planet.angle + planet.speed * deltaTime) % 360;
      
      // 计算3D位置
      const radian = newAngle * Math.PI / 180;
      const orbitX = planet.radius * Math.cos(radian);
      const orbitZ = planet.radius * Math.sin(radian);
      
      // 计算3D效果（近大远小）
      const frontAngle = Math.abs((newAngle + 90) % 180 - 90);
      const scale = 0.7 + 0.3 * (1 - frontAngle / 90);
      
      // 计算z-index
      const zIndex = Math.round(scale * 100);
      
      return {
        ...planet,
        angle: newAngle,
        zIndex,
        transform: `translate3d(${orbitX}px, 0, ${orbitZ}px) scale(${scale})`,
        size: planet.size * scale
      };
    });
    
    this.setData({ planets });
  },

  // 触摸事件
  handleTouchStart(e) {
    this.setData({
      isDragging: true,
      startX: e.touches[0].clientX,
      currentRotation: this.data.sunRotateY
    });
    this.stopAnimation();
  },

  handleTouchMove(e) {
    if (!this.data.isDragging) return;
    
    const currentX = e.touches[0].clientX;
    const deltaX = currentX - this.data.startX;
    const rotation = this.data.currentRotation + deltaX * 0.5;
    
    this.setData({ sunRotateY: rotation });
    this.updatePlanets();
  },

  handleTouchEnd() {
    this.setData({ isDragging: false });
    this.data.lastTime = 0;
    this.startAnimation();
  }
});